package top.suven.base.common.util;

import com.github.pagehelper.util.StringUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class FileUtils {

	public final static String MD5 = "MD5";
	public final static String SHA = "SHA";
	
	/**
	 * 获取文件md5/sha
	 * @param file
	 * @param messageDigestType MD5/SHA
	 * @return
	 */
	public static String digestForFile(File file,String  messageDigestType){
        int buffersize = 1024;
        FileInputStream fis = null;
        DigestInputStream dis = null;

        try {
        	if(file.exists()){
        		 //创建转换器和文件流
                MessageDigest messageDigest =MessageDigest.getInstance(messageDigestType);
                fis = new FileInputStream(file);
                dis = new DigestInputStream(fis,messageDigest);

                byte[] buffer = new byte[buffersize];
                //DigestInputStream实际上在流处理文件时就在内部就进行了一定的处理
                while (dis.read(buffer) > 0);
                //通过DigestInputStream对象得到一个最终的MessageDigest对象。
                messageDigest = dis.getMessageDigest();
                // 通过messageDigest拿到结果，也是字节数组，包含16个元素
                byte[] array = messageDigest.digest();
                // 同样，把字节数组转换成字符串
                StringBuilder hex = new StringBuilder(array.length * 2);
                for (byte b : array) {
                    if ((b & 0xFF) < 0x10){
                        hex.append("0");
                    }
                    hex.append(Integer.toHexString(b & 0xFF));
                }
                return hex.toString();
        	}
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @Title: createFilePath
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @param @param foldName    设定文件
     * @return void    返回类型
     * @throws
     */
    public static void createFilePath(String foldName){
        if (StringUtil.isNotEmpty(foldName)) {
            if (StringUtil.isNotEmpty(foldName)) {
                File file = new File(foldName);
                String chmod = "chmod  777 " + file;
                if (!file.exists()) {
                    file.mkdirs();
                    try {
                        Runtime.getRuntime().exec(chmod);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }


    /**
     *
     * @Title: createFilePath
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @param @param foldName    设定文件
     * @return void    返回类型
     * @throws
     */
    public static void createFile(String foldName,String fileName){
        if(StringUtil.isNotEmpty(foldName) && StringUtil.isNotEmpty(fileName)){
            File file = new File(foldName);
            String chmod = "chmod  777 " + file;
            if(!file.exists()){
                file.mkdirs();
                try {
                    Runtime.getRuntime().exec(chmod);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            File fileAddress = new File(file,fileName);
            if(!fileAddress.exists()){
                try {
                    file.createNewFile();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
